package com.caigou.modules.famous.ui.challenge

import android.os.Bundle
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.bus.BusKey
import com.caigou.comm.base.bus.LiveDataBus
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseActivity
import com.caigou.modules.famous.adpter.FamousPageAdapter
import com.caigou.modules.famous.databinding.ActivityFamousChallengeBinding
import com.caigou.modules.famous.ui.challenge.fragment.MyChallengeFragment
import com.caigou.modules.famous.ui.challenge.fragment.RewardRecordFragment
import com.caigou.modules.famous.widget.FamousTypeDialog
import com.google.android.material.tabs.TabLayoutMediator
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/12/10
 */
@Route(path = RouterPath.Famous.PAGE_CHALLENGE)
@AndroidEntryPoint
class FamousChallengeActivity : BaseActivity() {

    private lateinit var binding: ActivityFamousChallengeBinding

    @JvmField
    @Autowired(name = AppConstants.FAMOUS_TYPE)
    var famousType: Int = 1

    private val itemNames = mutableListOf(
        "我的挑战",
        "领取记录"
    )

    private val fragments = mutableListOf<Fragment>(
        MyChallengeFragment(),
        RewardRecordFragment(),
    )

    private val pagerAdapter: FamousPageAdapter by lazy {
        FamousPageAdapter(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityFamousChallengeBinding.inflate(layoutInflater)
        setContentView(binding.root)
        containerId = binding.famousChallengeLlContainer.id
        initView()
    }

    private fun initView() {
        ARouter.getInstance().inject(this)
        pagerAdapter.setList(fragments)
        binding.famousChallengeViewPager2.adapter = pagerAdapter
        binding.famousChallengeViewPager2.isUserInputEnabled = false
        TabLayoutMediator(binding.famousChallengeTabLayout, binding.famousChallengeViewPager2, true, true) { tab, position ->
            tab.text = itemNames[position]
        }.attach()
        binding.famousChallengeViewPager2.offscreenPageLimit = itemNames.size

        binding.famousChallengeTvTitle.run {
            text = if (famousType == 1) {
                "港彩名人堂"
            } else {
                "澳彩名人堂"
            }
            setOnClickListener {
                val dialog = FamousTypeDialog(this@FamousChallengeActivity, famousType)
                dialog.apply {
                    rgFamousType.setOnCheckedChangeListener { group, checkedId ->
                        famousType = when (checkedId) {
                            btnHkFamous.id -> 1
                            btnMacaoFamous.id -> 2
                            else -> 1
                        }
                        text = if (famousType == 1) {
                            "港彩名人堂"
                        } else {
                            "澳彩名人堂"
                        }
                        LiveDataBus.get().with(BusKey.CHALLENGE_TYPE_CHANGE, Int::class.java).value = famousType
                        dismiss()
                    }
                    show()
                }
            }
        }

        binding.famousChallengeTvRule.setOnClickListener {
            ARouter.getInstance().build(RouterPath.Famous.PAGE_RULE).navigation()
        }

        binding.famousChallengeTvBack.setOnClickListener {
            finish()
        }
    }
}